﻿//var cluster = require('cluster');
//var numCPUs = 8;//设定启动进程数为cpu个数
//if (cluster.isMaster) {
//  for (var i = 0; i < numCPUs; i++) {
//    cluster.fork();//启动子进程
//  }
//}else{

var express = require('express');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/manage/index');
var timeout = require('connect-timeout');
var partials = require('express-partials');
var settings = require("./config/db");
var config = require("./config/config");
var moment = require("moment");
//var proxy = require("./proxy");
var app = express();


app.all('*', function (req, res, next) {
  if (req.method == "OPTIONS") {
    return res.send('200');
  }else if(req.method == "HEAD") {
    return res.send('200');
  }else{
    next();
  }
});
//错误处理  待完善
//process.on('uncaughtException', function (err) {
//  console.log('Caught exception: ', err);
//});


app.locals.dateFormat = function(date){
  return moment(date).format('YYYY年MM月DD日HH:mm:ss');
};

//ue

var ueditor = require('./middle/upload');
app.use('/ueditor/ue', ueditor({
  configFile: '/ueditor/jsp/config.json',
  mode: 'qiniu', //本地存储填写local
  AccessKey : config.qiniu.ACCESS_KEY,
  SecrectKey : config.qiniu.SECRET_KEY,
  bucket : config.qiniu.bucket,
  staticPath: path.join(__dirname, 'public'),
  hostName : config.hostName,
  uploadPath: config.uploadPath,
}));

//redis
app.use(session({
  secret: settings.redis.secret,
  store: new RedisStore({
    port: settings.redis.port,//port: 6379
    host: settings.redis.host,
    pass : settings.redis.password,
    db : settings.redis.redis_db,
    ttl: 1800000 // 过期时间
  }),
  resave: true,
  saveUninitialized: false
}));




// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(partials());

//超时
//app.use(timeout('5s'));

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);





app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});


// 500错误处理
// app.use(function (err, req, res, next) {
//   if (err.status == 503) return res.json({
//     status: 503,
//     message: '服务器无响应'
//   });
//   res.json({
//     status: 500,
//     error: err.message
//   });
//   next();
// });

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err,
      category:'userManage_register'
    });
  });
}

app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: err,
    category:'userManage_register'
  });
});

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {},
    category:'userManage_register'
  });
});

module.exports = app;

// app.listen(3000);

//console.log('Web server has started on http://127.0.0.1:3000/');
